package com.batata.continuing.service.billing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.batata.continuing.dao.billing.BillingDao;
import com.batata.continuing.pojo.base.ResultData;
import com.batata.continuing.pojo.billing.BillInfo;
import com.batata.continuing.pojo.billing.BillParam;
import com.batata.continuing.service.common.CommonService;
import com.batata.continuing.service.tree.TreeService;
import com.batata.continuing.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;

import static com.batata.continuing.service.common.ResultCommon.resultDataCommon;

@Service
@Transactional
public class BillingService extends ServiceImpl<BillingDao, BillInfo> {


    @Autowired
    private CommonService commonService;

    @Autowired
    private TreeService treeService;

    /**
     * 添加账单信息
     *
     * @param billInfo 账单信息
     * @return
     */
    public ResultData<Object> addBilling(BillInfo billInfo, HttpServletRequest request) {

        String userId = commonService.getUserIdByRequest(request);

        billInfo.setUserId(new BigInteger(userId));
        billInfo.setBillTypeText(treeService.getNodeByTreeId(billInfo.getBillType()).getTreeName());

        this.baseMapper.insert(billInfo);

        return resultDataCommon(200, "新增成功", true);
    }

    /**
     * 账单列表
     *
     * @return
     */
    public ResultData<?> billingList(BillParam billParam, HttpServletRequest request) {
        String userId = commonService.getUserIdByRequest(request);

        Page<BillInfo> page = new Page<>(billParam.getCurrent(), billParam.getSize());

        QueryWrapper<BillInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_remove", 0); //是否删除

        if (
                StringUtils.isNotEmpty(billParam.getBillType())
        )
            queryWrapper.eq("bill_type", billParam.getBillType());

        queryWrapper.orderByDesc("jlsj");
        return resultDataCommon(200, "查询成功", true,
                this.baseMapper.selectPage(page, queryWrapper));
    }

    /**
     * 删除账单
     *
     * @param id      账单id
     * @param request 验证tokenid
     * @return
     */
    public ResultData<Object> removeBilling(BigInteger id, HttpServletRequest request) {
        String userId = commonService.getUserIdByRequest(request);

        UpdateWrapper<BillInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.eq("user_id", userId);
        updateWrapper.set("is_remove", 1);
        int update = this.baseMapper.update(null, updateWrapper);
        if (update == 1) {
            return resultDataCommon(200, "删除成功", true);
        } else {
            return resultDataCommon(500, "删除失败", false);
        }

    }
}
